var connection = require("../mysqldb");



class insertSql {
  constructor() { }
  init(res, data) {
    console.log("执行了插入数据库的操作");
    var jsonserver = {};
    var sql = "SELECT count(*) count FROM hhpwuser WHERE user = '" + data.user + "'";
    var coun;
    connection.query(sql, (err, result) => {
      console.log(result[0].count)
      if (result[0].count < 1) {
        var insql = "INSERT INTO `hhpwuser`(`user`, `password`) VALUES ('" + data.user + "','" + data.password + "')"

        connection.query(insql, (err, result) => {
          if (err) {
            console.log('[SELECT ERROR] - ', err.message);
            return;
          } else {
            console.log("插入成功", result);
            jsonserver = {
              "msg": "用户注册成功",
              "code": "1",
            }
            res.write("");
            res.end(JSON.stringify(jsonserver));
          }
        })

      } else {
        jsonserver = {
          "msg": "用户注册失败",
          "code": "0",
        }
        res.write("");
        res.end(JSON.stringify(jsonserver))
      }


    })

  }
}







/* 
var insertSql = {
  init: async function (res, data) {
    console.log("执行了插入数据库的操作");
    var jsonserver = {};

    var sql = "SELECT count(*) count FROM hhpwuser WHERE user = '" + data.user + "'";
    var coun;
    connection.query(sql, (err, result) => {
      console.log(result[0].count)
      if (result[0].count < 1) {
        var insql = "INSERT INTO `hhpwuser`(`user`, `password`) VALUES ('" + data.user + "','" + data.password + "')"

        connection.query(sql, (err, result) => {
          if (err) {
            console.log('[SELECT ERROR] - ', err.message);
            return;
          } else {
            console.log("插入成功", result);
            jsonserver = {
              "msg": "用户注册成功",
              "code": "1",
            }
            res.write("");
            res.end(JSON.stringify(jsonserver));
          }
        })

      } else {
        jsonserver = {
          "msg": "用户注册失败",
          "code": "0",
        }
        res.write("");
        res.end(JSON.stringify(jsonserver))
      }


    })

  }
} */

module.exports = new insertSql;